package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.UserRechargeableCardConsumeRequest;
import com.frogs.ucenter.v1.service.UserRechargeableCardService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "userRechargeableCardController")
@RequestMapping("/" + ApiConstants.VERSION )
public class UserRechargeableCardController extends BaseController3 {
	
	private static final Logger log = LoggerFactory.getLogger(UserRechargeableCardController.class);

	private static final String ROOT = "/user";
	private static final String USER_RECHARGEABLE_CARD_CONSUME = ROOT + "/rechargeableCard/consume";
	
	@Resource(name=ApiConstants.VERSION+"userRechargeableCardService")
	private UserRechargeableCardService userRechargeableCardService;
	
	/**
	 * 核销充值卡，并把金额添加入用户余额中
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_RECHARGEABLE_CARD_CONSUME)
    public Response userRechargeableCardConsume(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserRechargeableCardConsumeRequest userRechargeableCardRequest = new UserRechargeableCardConsumeRequest();
        	userRechargeableCardRequest.parse(request);
        	response = userRechargeableCardService.consume(userRechargeableCardRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }
	
}
